/*
 * Luu thong so cau hinh cho Polygon
 */
viettel.PolygonOptions = function() {
    this.CLASSNAME = "viettel.PolygonOptions";

    this.map = null;
    this.paths = null;                  // Luon luon la MVCArray<MVCArray<LatLng>>
    this.strokeColor = "#0000FF";
    this.strokeOpacity = 0.8;
    this.strokeWeight = 1;
    this.fillColor = '#0000FF';
    this.fillOpacity = 0.2;
    this.zIndex = null;
    this.clickable = true;
    this.draggable = false;             // Them phien ban 2.0
    this.editable = false;              // Them phien ban 2.0
    this.visible = true;                // Them phien ban 2.0
    
    // Tra ve style theo chuan OpenLayers tuong ung voi cac thuoc tinh
    this.getStyle = function() {
	var style = {
	    strokeColor: this.strokeColor,
	    strokeOpacity: this.strokeOpacity,
	    strokeWidth: this.strokeWeight,
	    strokeWeight: this.strokeWeight,
	    fillColor: this.fillColor,
	    fillOpacity: this.fillOpacity,
	    cursor: (this.clickable?"pointer":""),
	    display: (this.visible?null:"none")
	};
	if (this.zIndex!=null) {
	    style.graphicZIndex = this.zIndex.toString();
	}
	return style;
    }
    
    // Cap nhat mang toa do theo dung chuan (Ham nay ko thuc hien clone)
    this.updatePaths = function(paths) {
	if (paths==null) return false;
        
	var ret = false;
	var path0, path, p, pathNum, i, j;
	if (VTObjChecker.isMVCArray(paths) && paths.getLength()>=0) {
	    if (paths.getLength()==0) {
		this.paths = paths;
		return true;
	    }

	    pathNum = paths.getLength();
	    path0 = paths.getAt(0);
	    if (VTObjChecker.isMVCArray(path0) && path0.getLength()>0) {
		// Dinh dang MVCArray<MVCArray>
		if (VTObjChecker.isLatLngObj(path0.getAt(0))) {
		    //this.paths = paths;     // Su dung truc tiep
                    this.paths = new viettel.MVCArray();
                    // ??? Delete endpoint when endpoint like start point
                    for (i=0; i<pathNum; i++) {
			path = paths.getAt(i);
			if (path.getLength() > 2) {
                            if (path.getAt(0).equals(path.getAt(path.getLength()-1))) {
                                path.pop();
                            }
                        }
			this.paths.push(path);
		    }
		    ret = true;
		}
	    }
	    else if (VTObjChecker.isArray(path0) && path0.length>0) {
		// Dinh dang MVCArray<Array>
		if (VTObjChecker.isLatLngObj(path0.getAt(0))) {
		    this.paths = new viettel.MVCArray();
		    for (i=0; i<pathNum; i++) {
			path = paths.getAt(i);
			p = new viettel.MVCArray();
			for (j=0; j<path.length; j++) {
			    p.push(path[j]);
			}
                        // ??? Delete endpoint when endpoint like start point
                        if (p.getLength() > 2) {
                            if (p.getAt(0).equals(p.getAt(p.getLength()-1))) {
                                p.pop();
                            }
                        }
			this.paths.push(p);
		    }
		    ret = true;
		}
	    }
	    else if (VTObjChecker.isLatLngObj(path0)) {
		// Dinh dang MVCArray<LatLng>
		this.paths = new viettel.MVCArray();
		p = new viettel.MVCArray();
		for (i=0; i<pathNum; i++) {
		    p.push(paths.getAt(i));
		}
                // ??? Delete endpoint when endpoint like start point
                if (p.getLength() > 2) {
                    if (p.getAt(0).equals(p.getAt(p.getLength() - 1))) {
                        p.pop();
                    }
                }
		this.paths.push(p);
		ret = true;
	    }
	}
	else if (VTObjChecker.isArray(paths) && paths.length>=0) {
	    if (paths.length==0) {
		this.paths = new viettel.MVCArray();
		return true;
	    }
	    
	    pathNum = paths.length;
	    path0 = paths[0];
	    if (VTObjChecker.isMVCArray(path0) && path0.getLength()>0) {
		// Dinh dang Array<MVCArray>
		if (VTObjChecker.isLatLngObj(path0.getAt(0))) {
		    this.paths = new viettel.MVCArray();
		    for (i=0; i<pathNum; i++) {
			path = paths[i];
			p = new viettel.MVCArray();
			for (j=0; j<path.getLength(); j++) {
			    p.push(path.getAt(j));
			}
                        // ??? Delete endpoint when endpoint like start point
                        if (p.getLength() > 2) {
                            if (p.getAt(0).equals(p.getAt(p.getLength() - 1))) {
                                p.pop();
                            }
                        }
			this.paths.push(p);
		    }
		    ret = true;
		}
	    }
	    else if (VTObjChecker.isArray(paths[0]) && path0.length>0) {
		// Dinh dang Array<Array>
		if (VTObjChecker.isLatLngObj(path0[0])) {
		    this.paths = new viettel.MVCArray();
		    for (i=0; i<paths.length; i++) {
			path = paths[i];
			p = new viettel.MVCArray();
			for (j=0; j<path.length; j++) {
			    p.push(path[j]);
			}
                        // ??? Delete endpoint when endpoint like start point
                        if (p.getLength() > 2) {
                            if (p.getAt(0).equals(p.getAt(p.getLength() - 1))) {
                                p.pop();
                            }
                        }
			this.paths.push(p);
		    }
		    ret = true;
		}
	    }
	    else if (VTObjChecker.isLatLngObj(path0)) {
		// Dinh dang Array<LatLng>
		this.paths = new viettel.MVCArray();
		p = new viettel.MVCArray();
		for (i=0; i<pathNum; i++) {
		    p.push(paths[i]);
		}
                // ??? Delete endpoint when endpoint like start point
                if (p.getLength() > 2) {
                    if (p.getAt(0).equals(p.getAt(p.getLength() - 1))) {
                        p.pop();
                    }
                }
		this.paths.push(p);
		ret = true;
	    }
	}
        
	if (!ret) {
	    // Thong bao loi
	    VTUtil.throwVTError("This object is not one of Array<LatLng>, Array<Array<LatLng>>, MVCArray<LatLng>, MVCArray<MVCArray<LatLng>>"); 
	}
        
	return ret;
    }
    
    // Lay boundary vung truyen vao
    this.getBounds = function() {
	if (this.paths==null) return null;
	var b = new viettel.LatLngBounds();
	for (var i=0; i<this.paths.getLength(); i++) {
	    var p = this.paths.getAt(i);
	    for (var j=0; j<p.getLength(); j++) {
		b = b.extend(p.getAt(j));
	    }
	}
	return b;
    }
	
    this.setOptions = function(opts) {
	if (opts==null) return;
        
	// Xu ly cap nhat thuoc tinh path/paths
	if (opts.path!=null && opts.paths==null) {
	    opts.paths = opts.path;             // De ho tro cac phien ban cu
	}
	if (opts.paths != null) {
	    this.paths = opts.paths;
	}
	if (VTObjChecker.isBoolean(opts.clickable)) {
	    this.clickable = opts.clickable;
	}
	if (VTObjChecker.isBoolean(opts.draggable)) {
	    this.draggable = opts.draggable;
	}
	if (VTObjChecker.isBoolean(opts.editable)) {
	    this.editable = opts.editable;
	}
	var styleChanged = false;
	if (VTObjChecker.isBoolean(opts.visible)) {
	    this.visible = opts.visible;
	}
	if (VTObjChecker.isString(opts.fillColor)) {
	    this.fillColor = opts.fillColor;
	}
	if (VTObjChecker.isNumber(opts.fillOpacity)) {
	    this.fillOpacity = opts.fillOpacity;
	}
	if (VTObjChecker.isString(opts.strokeColor)) {
	    this.strokeColor = opts.strokeColor;
	}
	if (VTObjChecker.isNumber(opts.strokeOpacity)) {
	    this.strokeOpacity = opts.strokeOpacity;
	}
	if (VTObjChecker.isNumber(opts.strokeWeight)) {
	    this.strokeWeight = opts.strokeWeight;
	}
	if (VTObjChecker.isNumber(opts.zIndex)) {
	    this.zIndex = opts.zIndex;
	}
	if (VTObjChecker.isObject(opts.map)) {
	    this.map = opts.map;
	}
	else if (typeof(opts.map) == "undefined") {
	}
	else if (opts.map == null) {
	    this.map = null;
	}
    }
}